<?php 
    class StudentInformationRegisterBLL{

        //使用Excel获得的数据导入学生信息
        public function importExcelForStudent($instructor_id,$data){
            $flag=true;
            $indexes=array('A','B','C','D','E','F','G');
            $head=array('学号','姓名','性别','年级','专业','班级','联系电话');
            $publicData=new PublicDataBLL();
            $majorList=$publicData->getMajorList();
            $studentList=array();

            //验证格式
            $count=0;
            foreach($data[0] as $key=>$value){
                if(!$value==$head[$count]){
                    $flag=false;
                    break;
                }
                $count++;
            }

            if($flag){
                if(count($data)>=2){
                    for($i=1; $i<count($data); $i++){
                        $studentInfo=new StudentInfo();
                        $studentInfo->student_id=$data[$i]['A'];
                        $studentInfo->name=$data[$i]['B'];
                        $studentInfo->gender=$data[$i]['C']=='女'?1:2;
                        $studentInfo->grade=$data[$i]['D'];
                        for($j=0;$j<count($majorList);$j++){
                            if($majorList[$j]['major_name']==$data[$i]['E']){
                                $studentInfo->major=$majorList[$j]['major_id'];
                                break;
                            }
                        }
                        $studentInfo->class=$data[$i]['F'];
                        $studentInfo->phone=$data[$i]['G'];
                        $studentList[]=$studentInfo;
                    }
                    $message=$this->addStudentInformation($instructor_id,$studentList);
                }else{
                    $message=array(
                        'code'=>'2004',
                        'message'=>'导入的Excel没有学生信息'
                    );
                }
            }else{
                $message=array(
                    'code'=>'2004',
                    'message'=>'导入的Excel格式不正确'
                );
            }
            return $message;
        }

        //获取学生信息列表
        public function getStudentInformationList($instructor_id,$grade,$major,$class,$page,$pageSize,$search){
            $action=$this->getQueryMode($grade,$major,$class,$search);
            $studentInformationRegisterDAL=new StudentInformationRegisterDAL();
            $result=$studentInformationRegisterDAL->getStudentInformationList($action,$instructor_id,$grade,$major,$class,$page*$pageSize,$pageSize,$search);
            $pageMax=$this->getStudentInformationListPage($result['count'],$pageSize);
            $result['count']=$pageMax;
            return $result;
        }

        //获取页数
        public function getStudentInformationListPage($count,$pageSize){
            $pageMax=0;
            if($count>$pageSize){
                if($count%$pageSize===0){
                    $pageMax=(int)($count/$pageSize);
                }else{
                    $pageMax=(int)($count/$pageSize)+1;
                }
            }
            
            return $pageMax;
        }

        //获取查询模式
        public function getQueryMode($grade,$major,$class,$search){
            $action=0;
            if($grade==0&&$major==0&&$class==0){
                if($search==''){
                    $action=1;
                }else{
                    $action=2;
                }
            }else if($grade!=0&&$major==0&&$class==0){
                if($search==''){
                    $action=3;
                }else{
                    $action=4;
                }
            }else if($grade==0&&$major!=0&&$class==0){
                if($search==''){
                    $action=5;
                }else{
                    $action=6;
                }
            }else if($grade!=0&&$major!=0&&$class==0){
                if($search==''){
                    $action=7;
                }else{
                    $action=8;
                }
            }else if($grade!=0&&$major!=0&&$class!=0){
                if($search==''){
                    $action=9;
                }else{
                    $action=10;
                }
            }else if($class!=0&&$grade==0&&$major==0){
                if($search==''){
                    $action=11;
                }else{
                    $action=12;
                }
            }else if($class!=0&&$grade!=0&&$major==0){
                if($search==''){
                    $action=13;
                }else{
                    $action=14;
                }
            }else if($class!=0&&$grade==0&&$major!=0){
                if($search==''){
                    $action=15;
                }else{
                    $action=16;
                }
            }

            return $action;
        }

        //添加学生信息
        public function addStudentInformation($instructor_id,$studentList){
            $validateError=array();
            $studentError=array();
            //验证输入
            for($i=0;$i<count($studentList);$i++)
                $validateError[]=$this->inputValidate($studentList[$i]);
            //判断学号是否存在
            foreach($studentList as $student){
                if($this->isStudent($student->student_id)){
                    $studentError[]=$student->student_id;
                }
            }
            if(count($validateError,1)==count($validateError) && count($studentError)==0){
                $studentInformationRegisterDAL=new StudentInformationRegisterDAL();
                $result=$studentInformationRegisterDAL->addStudentInformation($instructor_id,$studentList);
                if($result){
                    $message=array(
                        'code'=>'2001',
                        'message'=>'添加成功'
                    );
                }else{
                    $message=array(
                        'code'=>'2004',
                        'message'=>'添加失败'
                    );
                }
            }else if(count($studentError)!=0){
                $message=array(
                    'code'=>'2002',
                    'message'=>'已存在学号',
                    'error'=>json_encode($studentError,JSON_UNESCAPED_UNICODE),
                );
            }else{
                $message=array(
                    'code'=>'2003',
                    'message'=>'输入不正确',
                    'error'=>json_encode($validateError,JSON_UNESCAPED_UNICODE),
                );
            }

            return $message;
        }
        //更新学生信息
        public function updateStudentInfomation($instructor_id,$studentList){
            $validateError=array();
            for($i=0;$i<count($studentList);$i++)
                $validateError[]=$this->inputValidate($studentList[$i]);

            if(count($validateError,1)==count($validateError)){
                $studentInformationRegisterDAL=new StudentInformationRegisterDAL();
                $result=$studentInformationRegisterDAL->updateStudentInfomation($instructor_id,$studentList);
                if($result){
                    $message=array(
                        'code'=>'2001',
                        'message'=>'修改成功'
                    );
                }else{
                    $message=array(
                        'code'=>'2004',
                        'message'=>'修改失败'
                    );
                }
            }else{
                $message=array(
                    'code'=>'2003',
                    'message'=>'输入不正确',
                    'error'=>json_encode($validateError,JSON_UNESCAPED_UNICODE)
                );
            }

            return $message;
        }

        //移除学生信息
        public function removeStudentInformation($instructor_id,$studentList){
            $studentInformationRegisterDAL=new StudentInformationRegisterDAL();
            $result=$studentInformationRegisterDAL->removeStudentInformation($instructor_id,$studentList);
            if($result){
                $message=array(
                    'code'=>'2001',
                    'message'=>'移除成功'
                );
            }else{
                $message=array(
                    'code'=>'2004',
                    'message'=>'移除失败'
                );
            }

            return $message;
        }

        //获取辅导员年级与专业列表
        public function getInstructorGradeAndMajorList($uid){
            $studentInformationRegisterDAL=new StudentInformationRegisterDAL();
            $result=$studentInformationRegisterDAL->getInstructorGradeAndMajorList($uid);

            return $result;
        }
        //判断学生是否存在
        public function isStudent($student_id){
            $flag=false;
            $studentInformationRegisterDAL=new StudentInformationRegisterDAL();
            $result=$studentInformationRegisterDAL->isStudent($student_id);
            if($result['count']>0){
                $flag=true;
            }

            return $flag;
        }

        //获取年级专业班级
        public function getStudentGradeMajorClass($instructor_id,$field){
            if($field=='grade'){
                $action=1;
            }else if($field=='major'){
                $action=2;
            }elseif($field=='class'){
                $action=3;
            }
            $studentInformationRegisterDAL=new StudentInformationRegisterDAL();
            $result=$studentInformationRegisterDAL->getStudentGradeMajorClass($action,$instructor_id);

            return $result;
        }

        //获取年级专业班级
        public function getInstructorGradeMajorClass($instructor_id,$field){
            if($field=='grade'){
                $action=1;
            }else if($field=='major'){
                $action=2;
            }elseif($field=='class'){
                $action=3;
            }
            $studentInformationRegisterDAL=new StudentInformationRegisterDAL();
            $result=$studentInformationRegisterDAL->getInstructorGradeMajorClass($action,$instructor_id);

            return $result;
        }

        public function getStudentGradeList(){

        }
        public function getStudentMajorList(){
            
        }
        public function getStudentClassList(){
            
        }

        public function inputValidate($object){
            $error=array();
            $validate=new Validate();
            foreach($object as $key=>$value){
                if(!$validate->regular($key,$value)){
                    $error[]=$key;
                }
            }
            return $error;
        }
    }
?>